package com.gxuwz.zlc.controller;

import com.gxuwz.zlc.common.Result;
import com.gxuwz.zlc.entity.Department;
import com.gxuwz.zlc.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    // 查询
    @RequestMapping("/findAll2")
    @ResponseBody
    public List<Department> findDepartment2(){
        List<Department> departmentList = departmentService.findDepartment();
        return departmentList;
    }

    // 添加
    @RequestMapping("/add2")
    @ResponseBody
    public Map addDepartment2(@RequestBody Department department){
        Map<String,String> map = new HashMap<>();
        try {
            departmentService.addDepartment(department);
            map.put("code","200");
            map.put("msg","信息添加成功");
        } catch (Exception e) {
            map.put("code","500");
            map.put("msg","添加失败，编号不能重复");
            e.printStackTrace();
        }
        return map;
    }


    // 修改
    @RequestMapping("/update")
    @ResponseBody
    public Map updateDepartment(@RequestBody Department department){
        departmentService.updateDepartment(department);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","修改成功");
        return map;
    }

    // 删除
    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteDepartment(@RequestParam(name = "deptId") String deptId){
        departmentService.deleteDepartment(deptId);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("mag","删除成功");
        return map;
    }

    /**
     * 查询一个
     * @param depId
     * @return
     */
    @GetMapping("/findOne/{depId}")
    @ResponseBody
    public Result get(@PathVariable String depId){
        Department departmentById = departmentService.findDepartmentById(depId);
        // code=0,msg=成功
        return Result.success(departmentById);
    }

}
